package com.module.admin.rest.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.rest.dao.RestInfoDao;
import com.module.admin.rest.pojo.RestInfo;
import com.module.admin.rest.service.RestInfoService;
import com.system.comm.model.Page;
import com.system.handler.model.ResponseFrame;
import com.system.handler.model.ResponseCode;

/**
 * rest_info的Service
 * @author yuejing
 * @date 2016-12-23 16:43:04
 * @version V1.0.0
 */
@Component
public class RestInfoServiceImpl implements RestInfoService {

	@Autowired
	private RestInfoDao restInfoDao;
	
	@Override
	public ResponseFrame save(RestInfo restInfo) {
		ResponseFrame frame = new ResponseFrame();
		RestInfo org = getByRsIdUrl(restInfo.getRsId(), restInfo.getUrl());
		if(org == null) {
			restInfoDao.save(restInfo);
		} else {
			restInfo = org;
		}
		frame.setBody(restInfo);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	private RestInfo getByRsIdUrl(Integer rsId, String url) {
		return restInfoDao.getByRsIdUrl(rsId, url);
	}

	@Override
	public RestInfo get(Integer riId) {
		return restInfoDao.get(riId);
	}

	@Override
	public ResponseFrame pageQuery(RestInfo restInfo) {
		ResponseFrame frame = new ResponseFrame();
		int total = restInfoDao.findRestInfoCount(restInfo);
		List<RestInfo> data = null;
		if(total > 0) {
			data = restInfoDao.findRestInfo(restInfo);
		}
		Page<RestInfo> page = new Page<RestInfo>(restInfo.getPage(), restInfo.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(Integer riId) {
		ResponseFrame frame = new ResponseFrame();
		restInfoDao.delete(riId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public List<RestInfo> findAll() {
		return restInfoDao.findAll();
	}
}